package com.bss.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bss.beans.DailyRevenue;
import com.bss.command.*;
import com.bss.dao.PaymentDao;
import com.bss.utilities.DateUtilities;

public class ReceiptRevenue extends HttpServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * initialization method for servlet
	 */
	public void init() {
	}

	/**
	 * The doget method of the servlet
	 * 
	 * @param request
	 *            HttpServletRequest object
	 * @param response
	 *            HttpServletResponse object
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
			java.io.IOException {

		response.getWriter().println("NO OUTPUT");
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// com.bss.framework.FrontController.trace("DATE: "+
		// request.getParameter("Date"));
		com.bss.framework.FrontController.trace("THIS IS OLD");
		sendReceiptRevenueInfo(request.getParameter("Date"), response);

	}

	protected void sendReceiptRevenueInfo(String dateTo,
			HttpServletResponse response) throws IOException {

		PrintWriter pw = response.getWriter();

		try {

			String dateFrom = null;

			String lastDate = PaymentDao.latestDailyRevenueReportDate();
			if (lastDate.equals(""))
				lastDate = PaymentDao.latestDailyAccountReportDate();

			dateFrom = DateUtilities.dbDateToCalDate(lastDate);

			int receiptAmount = 0;

			com.bss.framework.FrontController.trace("Date from " + dateFrom);

			if (DateUtilities.findNormalDateDiff(dateFrom, dateTo) <= 0) {
				com.bss.framework.FrontController.trace("fromDate:" + dateFrom
						+ "--date to:" + dateTo + "---sub:"
						+ DateUtilities.findNormalDateDiff(dateFrom, dateTo));
				pw.write("ERROR: Already updated Daily Revenue till "
						+ dateFrom + " !");
				return;
			}

			if (dateFrom == null || dateFrom.length() == 0) {
				pw.write("ERROR: Date is not valid !");
				return;
			}

			int startRcptId = PaymentDao.oldestReceiptNo(dateFrom, dateTo);

			int stopRcptId = PaymentDao.latestReceiptNo(dateFrom, dateTo);

			receiptAmount = PaymentDao.totalCollectionAmount(startRcptId,
					stopRcptId);

			FrontController.audit("Payment calculated for " + dateFrom + " to "
					+ dateTo + ", Receipt: " + startRcptId + " to "
					+ stopRcptId + ", Total amount: " + receiptAmount);

			pw.write("" + startRcptId + "," + stopRcptId + "," + receiptAmount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			pw.write("EXCEPTION");
		}

	}

}
